import GameLauncher from "../GameLauncher";
import { Constants } from "./Constants";

export namespace util{
    export interface Vector3Like{
        x:number,
        y:number,
        z:number,
    }

    // 返回位置,旋转,缩放
    export function unpackTransformByNumberArray(numberArray:number[]):[mw.Vector,mw.Rotation,mw.Vector]{
        if(!numberArray || numberArray.length != 9){
            console.error(`传入要转换的数组无法转换为transform`);
            return [null,null,null]
        }
        return [
            new mw.Vector(numberArray[0],numberArray[1],numberArray[2]),
            new mw.Rotation(numberArray[3],numberArray[4],numberArray[5]),
            new mw.Vector(numberArray[6],numberArray[7],numberArray[8]),
        ]
    }

    export function vector2World(vector: Vector3Like, worldPosition: Vector3Like, forward: Vector3Like, right: Vector3Like): mw.Vector {
        return new mw.Vector(
            vector.x * forward.x + vector.y * right.x + worldPosition.x,
            vector.x * forward.y + vector.y * right.y + worldPosition.y,
            vector.x * forward.z + vector.y * right.z + vector.z + worldPosition.z,
        )
    }

    export function randomNegative():number{
        return Math.random() * 2 - 1
    }

    export function lerp(a:number,b:number,t:number):number{
        return (b - a) * t + a
    }

    export function clamp(value:number, min:number, max:number):number{
        return Math.min(Math.max(value, min), max)
    }

    export function colorLerp(a:mw.LinearColor,b:mw.LinearColor,t:number):mw.LinearColor{
        return new mw.LinearColor(
            lerp(a.r,b.r,t),
            lerp(a.g,b.g,t),
            lerp(a.b,b.b,t)
        )
    }

    // 根据秒数转换成MM:SS
    export function toMM_SSText(second: number): string {
        let m = Math.floor(second / 60)
        let s = Math.floor(second % 60)
        return `${m < 10 ? `0${m}` : m}:${s < 10 ? `0${s}` : s}`
    }
    
    // 获取资源库Icon赋值到目标ImageUI
    export function loadAssetsIcon(guid: string, targetImage: mw.Image) {
        mw.assetIDChangeIconUrlRequest([guid]).then(()=>{
            let iconData = mw.getAssetIconDataByAssetID(guid);
            if (!iconData) return;
            targetImage.setImageByAssetIconData(iconData);
        })
    }
    
    // [0-1]
    export function pingPong(t: number): number {
        return Math.abs(t % 1 - 0.5) * 2
    }

    // 大数转缩写文本
    export function getBigNum(num: number): string {
        let str = "";
        if (num <= 9999) {
            str = Math.round(num) + "";
        } else if (num > 9999 && num <= 999999) {
            str = Math.floor(num / 1000) + "K";
        } else {
            str = (num / 1000000).toFixed(0) + "M";
        }
        return str;
    }

    // 将数组内指定元素替换至-1
    export function arrayElementReplaceToMinusOne(array:number[], targetValue:number){
        for(let i = 0;i < array.length;i++){
            if(array[i] != targetValue) continue
            array[i] = -1
            return
        }
    }

    // 将数组全部元素替换至-1
    export function arrayAllElementReplaceToMinusOne(array:number[]){
        for(let i = 0;i < array.length;i++){
            array[i] = -1
        }
    }

    // 填充数据到指定数组(如果有-1则替换-1)
    export function arrayPushElement(array:number[], value:number){
        for(let i = 0;i < array.length;i++){
            if(array[i] != -1) continue
            array[i] = value
            return
        }
        array.push(value)
    }

    // 获取临时color
    let __temporary_color = LinearColor.black
    export function getTemporaryLinearColor(r:number, g:number, b:number):LinearColor{
        __temporary_color.r = r
        __temporary_color.g = g
        __temporary_color.b = b
        return __temporary_color
    }

    // 获取临时vector3
    let __temporary_vector = Vector.zero
    export function getTemporaryVector(x:number, y:number, z:number):Vector{
        __temporary_vector.x = x
        __temporary_vector.y = y
        __temporary_vector.z = z
        return __temporary_vector
    }

    // 获取临时vector3
    let __temporary_vector2 = Vector2.zero
    export function getTemporaryVector2(x:number, y:number):Vector2{
        __temporary_vector2.x = x
        __temporary_vector2.y = y
        return __temporary_vector2
    }
}
